/*
 * Copyright 2004-2005 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.util.valuelist;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.asoft.common.base.model.BaseObject;

public class ValueListHandler
{
        static Logger logger = Logger.getLogger(ValueListHandler.class);

        private List valueList;

        private int countPerPage = 15;

        public ValueListHandler(Iterator valueIterator)
        {
                this.valueList = new Vector(100);
                while(valueIterator.hasNext())
                {
                        this.valueList.add(valueIterator.next());
                }
        }

        public ValueListHandler(Iterator valueIterator,
                                int countPerPage)
        {
                this.valueList = new Vector(100);
                while(valueIterator.hasNext())
                {
                        this.valueList.add(valueIterator.next());
                }
                this.countPerPage = countPerPage;
        }

        public ValueListHandler(List valueList)
        {
                this.valueList = valueList;
        }

        public ValueListHandler(List valueList,
                                int countPerPage)
        {
                this.valueList = valueList;
                this.countPerPage = countPerPage;
        }

        public int getPageCount()
        {
                int pageCount = this.getCount()/this.countPerPage;
                if(this.getCount()%this.countPerPage > 0)
                        pageCount ++;
                return pageCount;
        }


        // 取某页集合
        public List getOnePage(int pageNum)throws NoSuchPageException
        {
                int pageCount = this.getPageCount();
                // 无记录
                if(pageCount == 0)
                        return this.valueList;
                // 有记录,翻页
                if(pageCount < pageNum)
                {
                        throw new NoSuchPageException("翻页异常: 共" + pageCount + "页," + "无法翻到" + pageNum + "页");
                }
                // 开始翻页
                int fromIndex = this.countPerPage * (pageNum - 1);
                int toIndex = fromIndex + this.countPerPage;
                logger.debug("开始翻页(from: " + fromIndex + " to: " + toIndex + ")共" + this.getCount());
                if(toIndex > this.getCount())
                {
                        toIndex = this.getCount();
                }

                // 获取页面集合
                List destPageList = this.valueList.subList(fromIndex,toIndex);
                BaseObject prePreBO = null;
                // 取得前一个BO
                if(fromIndex != 0)
                {
                        prePreBO = (BaseObject)this.valueList.get(fromIndex - 1);
                }
                // 最后一个的下一个
                BaseObject nextBOofLastOne = null;
                if(toIndex != this.getCount() )
                {
                        nextBOofLastOne = (BaseObject)this.valueList.get(toIndex);
                }
                // bo相互关联
                for(int i = 0; i < destPageList.size(); i ++)
                {
                        BaseObject bo = (BaseObject)destPageList.get(i);
                        // setPreBO
                        if(i != 0)//
                        {
                                bo.setPreBO((BaseObject)destPageList.get(i - 1));
                        }
                        else
                        {
                                bo.setPreBO(prePreBO);
                        }
                        // setNextBO
                        if(i != (destPageList.size() - 1))
                        {
                                bo.setNextBO((BaseObject)destPageList.get(i + 1));
                        }
                        else
                        {
                                bo.setNextBO(nextBOofLastOne);
                        }
                }
                return destPageList;
        }

        // 取总记录条数
        public int getCount()
        {
                return this.valueList.size();
        }

        public List getList()
        {
                return this.valueList;
        }
}
